import 'package:flutter/material.dart';

class SearchBar extends StatefulWidget {
  final bool showLoaction; //展示位置按钮
  final Function goBackCallback; //回退按钮函数
  final String inputValue; // 搜索框输入值
  final String defaultInputValue; // 搜索框默认值
  final Function onCancel; //取消按钮final
  final bool showMap; //展示地图按钮final
  final Function onSearch; //用户点击搜索框触发final
  final Function onSearchSubmit;

  const SearchBar(
      {Key key,
      this.showLoaction,
      this.goBackCallback,
      this.inputValue = '111',
      this.defaultInputValue = '请输入搜索词',
      this.onCancel,
      this.showMap,
      this.onSearch,
      this.onSearchSubmit})
      : super(key: key);

  // 用户输入搜索词后，点击键盘的搜索键触发

  @override
  State<SearchBar> createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  String _searchWord = '';
  TextEditingController _controller;
  _onClean() {
    _controller.clear();
    setState(() {
      _searchWord = '';
    });
  }

  FocusNode _focus;

  @override
  void initState() {
    _controller = TextEditingController(text: widget.inputValue);
    _focus = FocusNode();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          if (widget.showLoaction != null)
            GestureDetector(
              onTap: () {},
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Icon(
                    Icons.room,
                    color: Colors.green,
                    size: 16,
                  ),
                  Text(
                    '北京',
                    style: TextStyle(color: Colors.green, fontSize: 14),
                  )
                ],
              ),
            ),
          if (widget.goBackCallback != null)
            Padding(
              padding: EdgeInsets.only(left: 5, right: 5),
              child: GestureDetector(
                onTap: () {},
                child: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.black,
                  size: 14,
                ),
              ),
            ),
          Expanded(
            child: Container(
              height: 34,
              decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.circular(60)),
              child: TextField(
                focusNode: _focus,
                onTap: () {
                  if (null == widget.onSearchSubmit) {
                    _focus.unfocus();
                  }
                  widget.onSearch();
                },
                onSubmitted: widget.onSearchSubmit,
                textInputAction: TextInputAction.search,
                controller: _controller,
                onChanged: (String value) {
                  setState(() {
                    _searchWord = value;
                  });
                },
                decoration: InputDecoration(
                    hintText: '请输入搜索词',
                    hintStyle: TextStyle(fontSize: 14.0),
                    border: InputBorder.none,
                    prefixIcon: Icon(
                      Icons.search,
                      color: Colors.grey,
                    ),
                    suffixIcon: GestureDetector(
                      onTap: _onClean,
                      child: Icon(
                        Icons.close,
                        color: Colors.grey,
                      ),
                    ),
                    contentPadding: EdgeInsets.only(top: -2)),
              ),
            ),
          ),
          if (widget.onCancel != null)
            Padding(
              padding: EdgeInsets.only(left: 5),
              child: GestureDetector(
                onTap: () {
                  widget.onCancel;
                },
                child: Text(
                  '取消',
                  style: TextStyle(fontSize: 14, color: Colors.black),
                ),
              ),
            ),
          if (widget.showMap != null)
            Padding(
              padding: EdgeInsets.only(left: 20),
              child: GestureDetector(
                onTap: () {},
                child: Image.asset(
                  'images/map.png',
                  width: 20,
                  height: 20,
                ),
              ),
            )
        ],
      ),
    );
  }
}
